package com.yhp.web;

import com.yhp.bean.Student;
import com.yhp.service.StudentService;
import com.yhp.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/Educational/student/getstudents"})
public class FindStudentServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");//设置post
        //1.接收参数 pageindex当前页码值
        String pageindex = req.getParameter("pageindex");
        int index = pageindex==null?1:Integer.parseInt(pageindex);
        //获得模糊查找条件
        String stuname=req.getParameter("stuname");
        String stuno=req.getParameter("stuno");
        String stusex=req.getParameter("stusex");
        int sex=stusex==null?-1:Integer.parseInt(stusex);

        //2.调取方法
        StudentService studentService = new StudentServiceImpl();
        List<Student> studentList=studentService.getall(index,5,stuname,stuno,sex);
        //总页数=总条数%每页显示条数>0?总条数/每页条数+1：总条数/每页条数  10/3=3
        //总条数
        int totalcount = studentService.totalcount(stuname,stuno,sex);
        //总页数
        int totalpage= totalcount %5>0? totalcount /5+1: totalcount /5;

        //3.跳转页面
        req.setAttribute("stulist",studentList);
        req.setAttribute("index",index);
        req.setAttribute("totalcount", totalcount);
        req.setAttribute("totalpage",totalpage);
        //存储模糊查条件
        req.setAttribute("sname",stuname);
        req.setAttribute("sno",stuno);
        req.setAttribute("ssex",sex);
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }
}
